home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3265 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  42 lines

  1. Newsgroups: comp.lang.c
  2. Path: howland.reston.ans.net!torn!nott!emr1!jagrant
  3. From: jagrant@emr1.emr.ca (John Grant)
  4. Subject: looping & sscanf
  5. Message-ID: <DLtqMw.8Ao@emr1.emr.ca>
  6. Organization: Energy, Mines, and Resources, Ottawa
  7. Date: Sat, 27 Jan 1996 05:16:08 GMT
  8.  
  9. In FORTRAN, I can read a set of numbers from a character string with an
  10. 'internal read':
  11.     character*100 buffer
  12.     real x(10)
  13.     read(buffer,*) (x(i),i=1,n)
  14.  
  15. How can I do this easily in C?  If I was reading directly from a file, I
  16. would use:
  17.     for(i=0;i<n;i++){
  18.       fscanf(f,"%g",&x[i]);
  19.     }
  20.  
  21. However, I want to read the values from a string, i.e. using atof() or
  22. sscanf() or ???.
  23.  
  24. I was hoping to do something like:
  25.     char buffer[100];
  26.     char *b=buffer;
  27.     for(i=0;i<n;i++){
  28.       sscanf(b,"%g",&x[i]);
  29.       b+=???;
  30.     }
  31. but I can't figure out how much to advance the pointer.
  32.  
  33. Perhaps I should loop through the string using strtok(), converting each
  34. substring using atof()? It seems a bit messy, well at least not as
  35. straightforward and simple as FORTRAN.
  36.  
  37. I'm sure it's straightforward, but I just can't see it (or find it in the FAQ).
  38. -- 
  39. John A. Grant                        jagrant@emr1.emr.ca
  40. Airborne Geophysics
  41. Geological Survey of Canada, Ottawa
  42.